package com.aia.prm.web.ria.app.ui.group;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.domain.VOGroup;
import com.aia.prm.domain.VOGroupExample;
import com.aia.prm.domain.VOPlan;
import com.aia.prm.persistence.dao.VOGroupDAO;
import com.aia.prm.persistence.dao.VOPlanDAO;
import com.aia.prm.web.ria.app.PrmApplication;
import com.cfh.util.Tools;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

@SuppressWarnings("serial")
public class GroupView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(GroupView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	GroupList GroupList = null;
	Form form = null;
	GroupContainer GroupContainer = null;
	GroupForm GroupForm = null;
	private VOGroupDAO GroupDAO = null;
	List<VOGroup> list = null;

	VerticalLayout topLayout = null;
	Button newButton = null;
	Button okButton = null;

	private ComboBox plansBox = null;

	public ComboBox getPlanBox() {
		return plansBox;
	}

	public GroupView(PrmApplication aapp) {
		app = aapp;
		setGroupDAO((VOGroupDAO) BeanFactory.get().getBean("vOGroupDAO"));
		init();
		buildMainLayout();
		load();
	}

	private void init() {
		plansBox = new ComboBox();
		VOPlanDAO vOPlanDAO = (VOPlanDAO) BeanFactory.get().getBean("vOPlanDAO");
		List list = vOPlanDAO.selectByExample(null);
		String firstItem = null;
		for (Object o : list) {
			VOPlan p = (VOPlan) o;
			if (firstItem == null) {
				firstItem = p.getPL_ID();
			}
			plansBox.addItem(p.getPL_ID());
			plansBox.setItemCaption(p.getPL_ID(), "[" + p.getPL_ID() + "]" + p.getPL_NAME());
		}
		if (firstItem != null) {
			plansBox.select(firstItem);
		}
	}

	void load() {

		String id = (String) plansBox.getValue();
		if (Tools.hasText(id)) {
			VOGroupExample example = new VOGroupExample();
			example.createCriteria().andPL_IDEqualTo(id);
			list = getGroupDAO().selectByExample(example);

		} else {
			list = new ArrayList();
		}
		if (GroupContainer == null) {
			try {
				GroupContainer = new GroupContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		GroupContainer.removeAllItems();
		GroupContainer.addAll(list);

		GroupList.setContainerDataSource(GroupContainer);

		GroupList.setVisibleColumns(GroupContainer.NATURAL_COL_ORDER);
		GroupList.setColumnHeaders(GroupContainer.COL_HEADERS_SHOW);

		GroupForm.setItemDataSource(null);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		newButton = new Button("添加记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				GroupForm.addObject();
			}
		});
		newButton.setIcon(new ThemeResource("icons/add.png"));
		
		// topLayout.addComponent(newButton);
		// topLayout.addComponent(plansBox);
		HorizontalLayout topLayoutSub = new HorizontalLayout();
		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(true);
		topLayoutSub.setSpacing(true);
		topLayoutSub.addComponent(new Label("选择产品:"));
		topLayoutSub.addComponent(plansBox);

		okButton = new Button("确定", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				logger.debug("选择了：" + plansBox.getValue());
				load();
			}
		});
		
		okButton.setIcon(new ThemeResource("icons/search.png"));
		topLayoutSub.addComponent(okButton);
		topLayoutSub.addComponent(newButton);

		topLayout.addComponent(topLayoutSub);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		GroupList = new GroupList(app, this);
		horiz.setFirstComponent(GroupList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		GroupForm = new GroupForm(this);

		rightLayout.addComponent(GroupForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = GroupList.getItem(GroupList.getValue());
		GroupForm.setItemDataSource(item);
	}

	public VOGroupDAO getGroupDAO() {
		return GroupDAO;
	}

	public void setGroupDAO(VOGroupDAO GroupDAO) {
		this.GroupDAO = GroupDAO;
	}

}
